<?php

//Wrapper for the facebook PHP SDK
require 'facebook/src/facebook.php';
    
class Myzen_Facebook extends Facebook{
	
	public function  __construct() {
		$config = Zend_Registry::get('dt_config')->facebook;
		if(empty($config)) throw new Myzen_Exception('faceebook key and secret must be configured in config to use');
		return parent::__construct(array(
  			'appId'  => $config->id,
  			'secret' => $config->secret,
		));
	} //end construct	
	
	
	public static function getUserDetails($userProfile) {

		$details = array();	
		$details['authDetails']['provider'] = 'facebook';		

		
		$details['authDetails']['user']['firstName'] = $userProfile['first_name'];	
		$details['authDetails']['user']['lastName'] = $userProfile['last_name'];	
		$details['authDetails']['user']['email'] = $userProfile['email'];
		//facebook stopped returning email so we need to do something with the username
		if(empty($details['authDetails']['user']['email'])) {
			if(!empty($userProfile['username'])) $details['authDetails']['user']['email'] = $userProfile['username'].'@facebook.com';
			else Throw new Myzen_Exception('please define your username on facebook to use the application, please visit https://www.facebook.com/username/');	
		} 
		
		$time = time();
		$details['authDetails']['signature']['time'] = $time;
		$details['authDetails']['signature']['value'] = Myzen_Utilities_UserDetails::generateSignature($details);
		return $details;
		
	
	}	//end getUserdetails
	
} //end Myzen_Facebook 